<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
    <link rel="stylesheet" href="../../../admin/css/reset.css"/>
</head>
<body class="pear-container">
<style>
    .layui-input-block input {
        width: 300px;
    }
</style>

<div class="layui-card">
    <div class="layui-card-body">

        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">基本信息</li>
                <li>安全设置</li>
            </ul>
            <div class="layui-tab-content">

                <!-- 基本信息 -->
                <div class="layui-tab-item layui-show">

                    <form class="layui-form" lay-filter="baseInfo">
                        <div class="layui-form-item">
                            <label class="layui-form-label">昵称</label>
                            <div class="layui-input-block">
                                <input type="text" name="real_name" required lay-verify="required" placeholder="请输入昵称" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">邮箱</label>
                            <div class="layui-input-block">
                                <input type="text" name="email" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">联系电话</label>
                            <div class="layui-input-block">
                                <input type="text" name="mobile_phone" placeholder="请输入联系电话" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="saveBaseInfo">
                                    提交
                                </button>
                                <button type="reset" class="pear-btn pear-btn-md">
                                    重置
                                </button>
                            </div>
                        </div>
                    </form>

                </div>

                <div class="layui-tab-item">

                    <form class="layui-form" action="">
                        <div class="layui-form-item">
                            <label class="layui-form-label">原始密码</label>
                            <div class="layui-input-block">
                                <input type="password" name="old_password" required lay-verify="required" placeholder="请输入原始密码" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">新密码</label>
                            <div class="layui-input-block">
                                <input type="password" name="password" required lay-verify="required" placeholder="请输入新密码" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">确认新密码</label>
                            <div class="layui-input-block">
                                <input type="password" name="password_confirm" required lay-verify="required" placeholder="请再次输入新密码" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="savePassword">
                                    提交
                                </button>
                                <button type="reset" class="pear-btn pear-btn-md">
                                    重置
                                </button>
                            </div>
                        </div>
                    </form>

                </div>

            </div>
        </div>

    </div>
</div>

<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script src="../../../lms/base64.js"></script>
<script src="../../../lms/common.js"></script>
<script src="../../../lms/permission.js"></script>
<script src="../../../lms/config.js"></script>
<script>
    const PRIMARY_KEY = "id";
    const SELECT_API = lms_config.api_url + '/api/account/info'
    const UPDATE_API = lms_config.api_url + '/api/account/update'
    const PASSWORD_API = lms_config.api_url + '/api/account/password'

    layui.use(["form", "popup", "encrypt"], function () {
        let form = layui.form;
        let $ = layui.$;
        let encrypt = layui.encrypt;
        let id = getUserInfo() != undefined ? getUserInfo()['info'][PRIMARY_KEY] : '';
        $.ajax({
            url: SELECT_API + '/' + id,
            dataType: "json",
            success: function (res) {
                form.val("baseInfo", res.data);
            }
        });

        form.on("submit(saveBaseInfo)", function (data) {
            data.field[PRIMARY_KEY] = id;
            $.ajax({
                url: UPDATE_API,
                dataType: "json",
                type: "POST",
                data: data.field,
                success: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    return layui.popup.success("操作成功");
                }
            });
            return false;
        });

        form.on("submit(savePassword)", function (data) {
            $.ajax({
                url: PASSWORD_API,
                dataType: "json",
                type: "POST",
                data: {
                    id: id,
                    old_password: encrypt.md5(data.field.old_password),
                    password: encrypt.md5(data.field.password),
                    password_confirm: encrypt.md5(data.field.password_confirm)
                },
                success: function (res) {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    return layui.popup.success("操作成功");
                }
            });
            return false;
        });

    });

</script>

</body>
</html>
